package com.tr.rentcar.controller.houtai.usermanage;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.tr.rentcar.common.StringUtils;
import com.tr.rentcar.model.bean.User;
import com.tr.rentcar.model.dao.UserDao;

@WebServlet("/houtaiuser")
public class UserManageController extends HttpServlet {
	User User = new User();
	UserDao UserDao = new UserDao();
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {String tel = req.getParameter("tel");
		List<User> userlist =null;
		if(tel==null||tel==""){
			userlist = UserDao.findAll();
		}else{
			userlist = UserDao.findByTelmohu(tel);
		}
			System.out.println(userlist);
			req.setAttribute("userlist", userlist);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		req.getRequestDispatcher("/WEB-INF/jsp/houtai_userManage.jsp").forward(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			req.setCharacterEncoding("utf-8");
			resp.setContentType("application/json;charset=utf-8");
				String id=req.getParameter("id");
				System.out.println(id);
			List<User> User =UserDao.findAll();
				req.setAttribute("User", User);
				PrintWriter out=resp.getWriter();
				out.print(JSONArray.toJSONString(User));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		//doGet(req, resp);
	}

}
